package org.example.springblog.config;

import lombok.extern.slf4j.Slf4j;
import org.example.springblog.constants.Constant;
import org.example.springblog.utils.JWTUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    //拦截器，进行所有的页面展示都进行拦截
    //写拦截器还要进行注册
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1.从header中得到token
        //2.进行校验token
        //3.放行
        String user_token=request.getHeader(Constant.USER_TOKEN_HEADER);
        Boolean token_flag= JWTUtils.checkToken(user_token);
        if(token_flag) {
            return true;
        }
        response.setStatus(401);
        return false;
    }
}
